﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.SSO.BizEntity;
using System.Web;
using System.Data;
using ZhiAo.CommonLibrary;
using ZhiAo.Management.Data;

namespace ZhiAo.Management
{
    public class UserInfo
    {
        private User _user;

        private DataTable _functions;        

        public User User
        {
            get
            {
                return _user;
            }
        }

        public DataTable Functions
        {
            get
            {
                return _functions;
            }
        }      

        public static UserInfo CurrentUserInfo
        {
            get
            {
                UserInfo ui = HttpContext.Current.Session["CurrentUserInfo"] as UserInfo;
                if (ui == null)
                {
                    throw new ApplicationException("请登录后操作");
                }
                return ui;
            }
        }

        public static bool Login(string userName, string password)
        {
            try
            {
                bool result = false;
                HttpContext.Current.Session["CurrentUserInfo"] = null;
                SsoServiceClient sso = new SsoServiceClient();
                User user = sso.Login(userName, password);
                sso.Close();
                if (user != null)
                {
                    UserInfo ui = new UserInfo();
                    ui._user = user;
                    
                    //获取该用户所有功能点
                    //ui._functions = UserQuery.GetFunctions(user.ID);
                    
                    HttpContext.Current.Session["CurrentUserInfo"] = ui;
                    result = true;
                }
                else
                {
                    result = false;
                }
                
                //存入Cookie
                CookieHelper.SetCookie("userName", userName);
                CookieHelper.SetCookie("password", password);

                return result;
            }
            catch (Exception ex)
            {
                HttpContext.Current.Session["CurrentUserInfo"] = null;
                throw ex;
            }
        }       
    }
}
